package com.choosefine.it.worker.controller;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.choosefine.it.base.BaseController;
import com.choosefine.it.base.dto.PageDTO;
import com.choosefine.it.common.def.CommonConstant;
import com.choosefine.it.common.def.ResultCode;
import com.choosefine.it.common.exception.BusinessException;
import com.choosefine.it.payment.model.TeamMembersVO;
import com.choosefine.it.project.model.vo.WorkerSalaryChangeDetailsVO;
import com.choosefine.it.worker.model.WorkerSalaryDayInterface;
import com.choosefine.it.worker.service.WorkerSalaryDayInterfaceService;
import com.choosefine.it.worker.vo.WorkerSalaryDayInterfaceExplainVO;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonMappingException;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;

/**
 * 11.26 dzq 工人工资日表
 * @author Administrator
 *
 */
@RestController
@RequestMapping("/worker/workerSalaryDayInterface")
@Api(value="工人工资日表",description="廖仉根")
public class WorkerSalaryDayInterfaceController extends BaseController{
    @Autowired
    private WorkerSalaryDayInterfaceService workerSalaryDayInterfaceService;
	
    /**
	 * 工作人-工作管理-我的工作-工作详情-每月下的明细（天）列表
	 * 
	 * @param userCode
	 */
	@ResponseBody
	@ApiOperation(value="工人角色-工作管理-我的工作-工作详情-每月下的明细（天）列表",notes="负责人：廖仉根|QQ:791855010")
	@RequestMapping(method = RequestMethod.GET, value = "/detail/day", produces = MediaType.APPLICATION_JSON_VALUE)
	public String queryWorkerSalaryDay(	@RequestParam(value="userCode",required=true) String userCode,
										@RequestParam(value="workerTeamRelationId",required=true) Integer workerTeamRelationId,
										@RequestParam(value="workerSalaryMaonthId",required=true) Integer workerSalaryMaonthId) throws BusinessException{
		String result = null;
		WorkerSalaryDayInterface wsdi = new WorkerSalaryDayInterface();
		wsdi.setUserCode(userCode);
		wsdi.setWorkerItemsRelationId(workerTeamRelationId);
		wsdi.setWorkerSalaryMonthId(workerSalaryMaonthId);
		
		List<WorkerSalaryDayInterface> wsdis= workerSalaryDayInterfaceService.queryWorkerSalaryDayDetails(wsdi);
		result =this.responseSuccess(wsdis);
		return result;
	}
    
    /**
     *   用于查出某（班组）团队下某时间段内的工人名单
     * 		update code by liaozhanggen 2016-12-29
     * @param
     * @return
     */
    @ApiOperation(value="班组下工人成员名单")
    @RequestMapping(value="/team/members/{teamId}",method=RequestMethod.GET)
    public String  selectTeamMemberUserByTeamId(@PathVariable(name="teamId",required=true)int teamId){
    	Map<String, Object> map =new HashMap<String, Object>();
    	map.put("teamId", teamId);
    	
    	List<Map<String, Object>> result = workerSalaryDayInterfaceService.selectTeamMemberUserByTeamId(map);
    	return responseSuccess(result);
    }
    
    /**
     * 11.26dzq 用于查出某条关系在某段时间下的工资日单
     * @param
     * @return
     */
	@GetMapping(value="/salary/detail")
    @ApiOperation(value="某工人某月份下工资日表、不分页",response=WorkerSalaryDayInterfaceExplainVO.class)
	@ApiImplicitParams({
		    @ApiImplicitParam(name="workerSalaryMonthId",value="月份关系id",dataType="int",required=true,paramType = "query"),
			@ApiImplicitParam(name = "userCode", value = "用户编码", required = false, dataType = "String", paramType = "query"),
			@ApiImplicitParam(name = "workerItemsRelationId", value = "工人所在项目-班组关联表id", required = true, dataType = "Integer", paramType = "query")
	})
	@ApiResponses({
		@ApiResponse(code = ResultCode.SUCCESS_CREATED, message = "添加成功返回结果"),
		@ApiResponse(code = ResultCode.FAILED, message = CommonConstant.PARAMETER_ERROR) })
   public String selectUserByRelation(
    		@RequestParam(name="workerSalaryMonthId",required=true)int workerSalaryMonthId,
			@RequestParam(name="userCode",required=false) String userCode,
			@RequestParam int workerItemsRelationId
		   ) {
		
		if (workerSalaryMonthId <= 0) {
    		return responseFail(CommonConstant.PARAMETER_ERROR);
		}

    	Map<String, Object> map =new HashMap<String, Object>();
    	map.put("workerSalaryMonthId", workerSalaryMonthId);
    	map.put("userCode",userCode);
    	map.put("workerItemsRelationId",workerItemsRelationId);
    	return responseSuccess(workerSalaryDayInterfaceService.selectDaySalaryDetailByMonthly(map));
    }

    /** 
     * @Description：项目管理-班组资金管理-工人工资管理-工人工资明细-8082月份列表
     * @Author：yuxin
     * @Create Date：2017/2/25/0025
     * @param：
     * @return：
     */
	@ApiOperation(value="项目管理-班组资金管理-工人工资管理-工人工资明细-8082月份统计",response=WorkerSalaryDayInterfaceExplainVO.class)
	@RequestMapping(method = RequestMethod.GET,value = "/selectmonthlist",produces = MediaType.APPLICATION_JSON_VALUE)
	@ApiImplicitParams({
			@ApiImplicitParam(name="date",value="日期如2017年02月",dataType="String",required=true,paramType = "query"),
			@ApiImplicitParam(name = "userCode", value = "用户编码", required = true, dataType = "String", paramType = "query"),
			@ApiImplicitParam(name = "workerItemsRelationId", value = "工人所在项目-班组关联表id", required = true, dataType = "Integer", paramType = "query")
	})
	public String selectmonthlist(
			@RequestParam String date,
			@RequestParam String userCode,
			@RequestParam int workerItemsRelationId
    		) {
		Map<String, Object> map =new HashMap<String, Object>();
		map.put("date", date);
		map.put("userCode",userCode);
		map.put("workerItemsRelationId",workerItemsRelationId);
		return responseSuccess(workerSalaryDayInterfaceService.selectmonthlist(map));
	}
	
	 /**
     * 11.26dzq 工人/班组开工最小时间
     * @param
     * @return
     */
	@GetMapping(value="/minTime")
    @ApiOperation(value="工人/班组开工最小时间",response=Long.class)
	@ApiImplicitParams({
		@ApiImplicitParam(
				name="relation",value="关系id",dataType="int",required=false,defaultValue="0",paramType = "query"),
		@ApiImplicitParam(
				name="teamId",value="班组",dataType="int",required=false,defaultValue="0",paramType = "query"),
	})
   public String selectMinTime(
    		@RequestParam(name="relation",defaultValue="0")int relation,
    		@RequestParam(name="teamId",defaultValue="0")int teamId,
    		PageDTO pageDTO) throws JsonParseException, JsonMappingException, JsonProcessingException, IOException{
    	if(relation<=0&&teamId<=0){
    		return responseFail(ResultCode.MSG_CREATE_ERROR);
		}
    	Map<String, Object> map =new HashMap<String, Object>();
    	map.put("relation", relation);
    	map.put("teamId", teamId);
    	return responseSuccess(workerSalaryDayInterfaceService.selectMinTime(map));
    }
	
    @ApiOperation(value = "单个工人薪资工资明细",response = WorkerSalaryChangeDetailsVO.class)
	@RequestMapping(value = "/workerSalaryDayDetails/{id}",method = RequestMethod.GET)
	public  String getWorkerSalaryChangeDetails(@PathVariable int id) throws BusinessException{
    	
		return responseSuccess(workerSalaryDayInterfaceService.findWorkerSalaryDayDetail(id));
	}
}
